<template>
    <div>
        从store中取出来的num值为：{{num}},
        加的值为：<input v-model="addnum" type="number">
        <button @click="add()">点击我+{{addnum}}</button>
        <button @click="addAsync(1)">点击我异步+{{addnum}}</button>
        <button @click="addAsync(2)">点击我异步-{{addnum}}</button>
        <button @click="addAsync(3)">点击我异步+{{addnum}}然后再减{{addnum}}</button>
        <button @click="getCount">点击我获取state中的count值</button>

    </div>
</template>

<script>

export default {
    name: 'Test',
    data(){
        return{
            //num: 0
            addnum: 1
        }
    },
    computed:{
        num(){
            return this.$store.state.count;
        }
    },
    created(){
        
    },
    methods: {
        add(){
            let val = new Number(this.addnum);
            this.$store.commit('addOne',val)
        },
        addAsync(type){
            let val = new Number(this.addnum);
            switch(type){
                case 1:
                    this.$store.dispatch('addOneAsync',val)
                    break;
                case 2:
                    this.$store.dispatch('decreaseAsync',val)
                    break;
                case 3:
                    this.$store.dispatch('addAndDecreaseAsync',val)
                    break;

            }

        },
        getCount(){
            alert(this.$store.getters.getCount)
        }
        
    }
}
</script>

